/*     */ package com.zimbra.cs.filter.jsieve;
/*     */ 
/*     */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*     */ import java.text.DateFormat;
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.ListIterator;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import org.apache.jsieve.Argument;
/*     */ import org.apache.jsieve.Arguments;
/*     */ import org.apache.jsieve.SieveContext;
/*     */ import org.apache.jsieve.StringListArgument;
/*     */ import org.apache.jsieve.TagArgument;
/*     */ import org.apache.jsieve.exception.SieveException;
/*     */ import org.apache.jsieve.exception.SyntaxException;
/*     */ import org.apache.jsieve.mail.MailAdapter;
/*     */ import org.apache.jsieve.tests.AbstractTest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DateTest
/*     */   extends AbstractTest
/*     */ {
/*  47 */   static DateFormat mShortDateFormat = new SimpleDateFormat("yyyyMMdd");
/*     */   static final String BEFORE = ":before";
/*     */   static final String AFTER = ":after";
/*     */   
/*     */   protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context)
/*     */     throws SieveException
/*     */   {
/*  54 */     String comparator = null;
/*  55 */     Date date = null;
/*     */     
/*  57 */     ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
/*     */     
/*     */ 
/*  60 */     if (argumentsIter.hasNext())
/*     */     {
/*  62 */       Object argument = argumentsIter.next();
/*  63 */       if ((argument instanceof TagArgument))
/*     */       {
/*  65 */         String tag = ((TagArgument)argument).getTag();
/*  66 */         if ((tag.equals(":before")) || (tag.equals(":after"))) {
/*  67 */           comparator = tag;
/*     */         } else {
/*  69 */           throw new SyntaxException("Found unexpected: \"" + tag + "\"");
/*     */         }
/*     */       }
/*     */     }
/*  73 */     if (null == comparator) {
/*  74 */       throw new SyntaxException("Expecting \":before\" or \":after\"");
/*     */     }
/*     */     
/*  77 */     if (argumentsIter.hasNext())
/*     */     {
/*  79 */       Object argument = argumentsIter.next();
/*  80 */       if ((argument instanceof StringListArgument)) {
/*  81 */         StringListArgument strList = (StringListArgument)argument;
/*  82 */         String datestr = (String)strList.getList().get(0);
/*     */         try {
/*  84 */           date = mShortDateFormat.parse(datestr);
/*     */         }
/*     */         catch (ParseException e) {}
/*     */       }
/*     */     }
/*     */     
/*  90 */     if (null == date) {
/*  91 */       throw new SyntaxException("Expecting a valid date (yyyyMMdd)");
/*     */     }
/*     */     
/*  94 */     if (argumentsIter.hasNext()) {
/*  95 */       throw new SyntaxException("Found unexpected argument(s)");
/*     */     }
/*  97 */     if (!(mail instanceof ZimbraMailAdapter))
/*  98 */       return false;
/*  99 */     return test(mail, comparator, date);
/*     */   }
/*     */   
/*     */ 
/*     */   protected void validateArguments(Arguments arguments, SieveContext context) {}
/*     */   
/*     */ 
/*     */   private boolean test(MailAdapter mail, String comparator, Date date)
/*     */     throws SieveException
/*     */   {
/* 109 */     MimeMessage mimeMsg = ((ZimbraMailAdapter)mail).getMimeMessage();
/*     */     try {
/* 111 */       Date msgDate = mimeMsg.getSentDate();
/* 112 */       if (msgDate == null)
/*     */       {
/* 114 */         msgDate = new Date();
/*     */       }
/* 116 */       if (":before".equals(comparator))
/* 117 */         return msgDate.before(date);
/* 118 */       if (":after".equals(comparator)) {
/* 119 */         return msgDate.after(date);
/*     */       }
/*     */     } catch (MessagingException e) {
/* 122 */       throw new SieveException(e.getMessage());
/*     */     }
/*     */     
/* 125 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/DateTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */